//-----------------------------------------------------------------------
// <copyright file="IDSSortDAO.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
	
using System;
using System.Data;
using MIMS.WRS.Entities;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MIMS.WRS.Data
{	
	public class ShoppingUserDAO 
	{
		public ShoppingUserDAO()
		{
		}

		#region Common methods
		public virtual ShoppingUser CreateShoppingUserFromReader(IDataReader reader)
		{
			ShoppingUser item = new ShoppingUser();
			try
			{
				if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (int)reader["UserId"];
				if (!reader.IsDBNull(reader.GetOrdinal("Email"))) item.Email = (string)reader["Email"];
				if (!reader.IsDBNull(reader.GetOrdinal("Password"))) item.Password = (string)reader["Password"];
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateShoppingUserFromReaderException, ex);
			}
			return item;
		}
		#endregion
        
		#region CreateShoppingUser methods
            
		public virtual void CreateShoppingUser(ShoppingUser shoppingUser)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingUserInsert");
                
				dbCommandWrapper.AddInParameter("@Email", DbType.String, shoppingUser.Email);
				dbCommandWrapper.AddInParameter("@Password", DbType.String, shoppingUser.Password);
				dbCommandWrapper.AddOutParameter("@UserId", DbType.Int32, 0);
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
				shoppingUser.UserId = (int)dbCommandWrapper.GetParameterValue("@UserId");
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateShoppingUserException, ex);
			}
		}


		public virtual void UpdateShoppingUser(ShoppingUser shoppingUser)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingUserUpdate");            
                
				dbCommandWrapper.AddInParameter("@UserId", DbType.Int32, shoppingUser.UserId);
				dbCommandWrapper.AddInParameter("@Email", DbType.String, shoppingUser.Email);
				dbCommandWrapper.AddInParameter("@Password", DbType.String, shoppingUser.Password);
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessUpdateUSERException, ex);
			}
		}
        
		#endregion

		#region GetShoppingUser methods
        
		public virtual ShoppingUser GetShoppingUserGetByEmail(string email)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingUserGetByEmail");
                
				dbCommandWrapper.AddInParameter("@Email", DbType.String, email);
                
				ShoppingUser shoppingUser =  null;
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					if (reader.Read())
					{
						shoppingUser = CreateShoppingUserFromReader(reader);
						reader.Close();
					}
				}
				return shoppingUser;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetShoppingListException, ex);
			}
		}
        
		#endregion
	}
}
